package com.wingice.system.controller;


import com.wingice.common.web.ErrorCodeInfo;
import com.wingice.system.entity.SysRole;
import com.wingice.system.entity.SysUser;
import com.wingice.system.exception.ResultBean;
import com.wingice.system.exception.SysBusinessCode;
import com.wingice.system.exception.SysBusinessException;
import com.wingice.system.service.SysRoleUserService;
import com.wingice.system.service.SysUserService;
import com.wingice.system.validator.sys.roleuser.SysRoleUserValidator;
import com.wingice.system.vo.UserRoleResourceVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色-用户关联 前端控制器
 * </p>
 *
 * @author 胡昊
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/sysRoleUser")
@Tag(name = "角色-用户管理")
public class SysRoleUserController {

    @Resource
    private SysRoleUserService sysRoleUserService;
    @Resource
    private SysUserService sysUserService;

    /**
     * @param userRoleResourceVo 用户角色参数
     * @description 保存用户的角色
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/26 15:21:03
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysRoleUser:saveUserRole')")
    @PostMapping("/saveUserRole")
    @Operation(summary = "保存用户角色")
    public ResultBean<Boolean> saveUserRole(@RequestBody UserRoleResourceVo userRoleResourceVo) {
        //参数校验
        SysRoleUserValidator.saveUserRoleValidate(userRoleResourceVo);
        return ResultBean.restResult(sysRoleUserService.saveUserRole(userRoleResourceVo), ErrorCodeInfo.OK);
    }

    /**
     * @param userId 用户id
     * @description 获取用户下角色
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/7/27 9:56
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysRoleUser:listUserRole')")
    @GetMapping("/listUserRole")
    @Operation(summary = "获取用户下角色")
    public ResultBean<List<SysRole>> listUserRole(String userId) {
        if (!StringUtils.hasText(userId)) {
            throw new SysBusinessException(SysBusinessCode.USER_ID_EMPTY_ERROR);
        }
        return ResultBean.restResult(sysRoleUserService.listUserRole(userId), ErrorCodeInfo.OK);
    }

    /**
     * @param userRoleResourceVo 角色用户参数
     * @description 保存角色用户
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/8/7 16:43:05
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysRoleUser:saveRoleUser')")
    @PostMapping("/saveRoleUser")
    @Operation(summary = "保存角色用户")
    public ResultBean<Boolean> saveRoleUser(@RequestBody UserRoleResourceVo userRoleResourceVo) {
        //参数校验
        SysRoleUserValidator.saveRoleUserValidate(userRoleResourceVo);
        return ResultBean.restResult(sysRoleUserService.saveRoleUser(userRoleResourceVo), ErrorCodeInfo.OK);
    }

    /**
     * @param roleId 角色id
     * @description 获取角色下用户
     * @author 胡昊
     * @email huhao9277@gmail.com
     * @date 2020/8/7 16:32:47
     */
    @PreAuthorize("@knifeSecurity.hasPermission('sysRoleUser:listRoleUser')")
    @GetMapping("/listRoleUser")
    @Operation(summary = "获取角色下用户")
    public ResultBean<List<SysUser>> listRoleUser(String roleId) {
        if (!StringUtils.hasText(roleId)) {
            throw new SysBusinessException(SysBusinessCode.ROLE_ID_EMPTY_ERROR);
        }
        return ResultBean.restResult(sysRoleUserService.listRoleUser(roleId), ErrorCodeInfo.OK);
    }
}

